import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/inspec/ins_task_detail_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/responsibility/security_responsibility_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

class TaskListController extends GetxController {
  /// 外部controller
  final externalController = Get.find<SecurityResponsibilityController>();

  /// 搜索使用key
  GlobalKey<CustomEasyRefreshListState>? globalKey;

  /// 请求参数
  Map<String, dynamic> queryParameters = {};

  // 初始化数据
  void initData(GlobalKey<CustomEasyRefreshListState> globalKey) {
    this.globalKey = globalKey;
  }

  /// 获取任务列表数据
  Future<List<InsTaskDetailEntity>> getListData(
    int pageNum,
    int pageSize, {
    required int type,
    String? search,
  }) async {
    queryParameters['pageNum'] = pageNum;
    queryParameters['pageSize'] = pageSize;
    queryParameters['taskContent'] = search;
    queryParameters['workTypeDicId'] = externalController.workTypeDicIdMap[type];

    // 获取产品列表接口
    final res = await HttpUtil()
        .generateGetRequest<List<InsTaskDetailEntity>>(
          Interface.obtainResponsibilityTasks,
          queryParameters: queryParameters,
        )
        .execute();
    List<InsTaskDetailEntity>? data;
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data ?? [];
  }

  /// 进入详情页面
  void toDetailsPage(String id) {
    Get.toNamed(AppRoutes.hazardSupplementaryDetailsPage, arguments: {
      'title': '任务履职',
      'taskId': id,
    })?.then((value) {
      if (value != null) {
        refreshPage();
      }
    });

    // Get.toNamed(AppRoutes.responsibilityTaskDetails, arguments: id)?.then((value) {
    //   if (value != null) {
    //     refreshPage();
    //   }
    // });
  }

  /// 搜索
  void refreshPage() {
    globalKey?.currentState?.onRefresh();
  }
}
